package com.ouss.tabs;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class MainMenu extends Activity {
	/** Called when the activity is first created. */
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.main);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_bar);
        TextView title = (TextView) findViewById(R.id.titlebar);
		Resources res = getResources();
		title.setText(res.getString(R.string.txt_barmainmenu));
		
		
		GridView gridview = (GridView) findViewById(R.id.gridview);
		gridview.setAdapter(new ImageAdapter(this));

		gridview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				Bundle args = new Bundle();
				switch (position) {
				case 0:
					Intent iInter = new Intent(MainMenu.this,
							TabsActivity.class);
					args.putString("ViewName", "Concours International");
					args.putSerializable("Tab1", (new OptionsTab("Tab1", "http://www.promosport.sport.tn/index.php?rid=68", false, false)));
					args.putSerializable("Tab2", (new OptionsTab("Tab2", "http://www.promosport.sport.tn/index.php?rid=32", true, false)));
					iInter.putExtras(args);
					
					startActivity(iInter);
					break;
				case 1:
					Intent iNatio = new Intent(MainMenu.this,
							TabsActivity.class);
					
					args.putString("ViewName", "Concours National");
					args.putSerializable("Tab1", (new OptionsTab("Tab1", "http://www.promosport.sport.tn/index.php?rid=62", false, false)));
					args.putSerializable("Tab2", (new OptionsTab("Tab2", "http://www.promosport.sport.tn/index.php?rid=31", true, false)));
					iNatio.putExtras(args);
					
					startActivity(iNatio);
					break;
				case 2:
					Intent iProm = new Intent(MainMenu.this,
							TabsActivity.class);
					
					args.putString("ViewName", "Concours PromoGoal");
					args.putSerializable("Tab1", (new OptionsTab("Tab1", "http://www.promosport.sport.tn/index.php?rid=153", false, true)));
					args.putSerializable("Tab2", (new OptionsTab("Tab2", "http://www.promosport.sport.tn/index.php?rid=149", true, true)));
					iProm.putExtras(args);
					
					startActivity(iProm);
					break;
				case 3:
					
					break;
				default:
					break;
				}
			}
		});

	}

	public class ImageAdapter extends BaseAdapter {
		private Context mContext;

		public ImageAdapter(Context c) {
			mContext = c;
		}

		public int getCount() {
			return mThumbIds.length;
		}

		public Object getItem(int position) {
			return null;
		}

		public long getItemId(int position) {
			return 0;
		}

		// create a new ImageView for each item referenced by the Adapter
		public View getView(int position, View convertView, ViewGroup parent) {
			
			View MyView = convertView;
			
			if (convertView == null) { // if it's not recycled, initialize some
										// attributes
				//Inflate the layout
	            LayoutInflater li = getLayoutInflater();
	            MyView = li.inflate(R.layout.grid_item, null);
	            
	            // Add The Text!!!
	            TextView tv = (TextView)MyView.findViewById(R.id.grid_item_text);
	            tv.setText(""+nThumbIds[position]);
	            
	            // Add The Image!!!           
	            ImageView iv = (ImageView)MyView.findViewById(R.id.grid_item_image);
	            //iv.setLayoutParams(new GridView.LayoutParams(85, 85));
	            //iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
	            //iv.setPadding(8, 8, 8, 8);
	            iv.setImageResource(mThumbIds[position]);
	            
			} 
			
			// return imageView;
			
            return MyView;
		}

		// references to our images
		private Integer[] mThumbIds = { R.drawable.international,
				R.drawable.national, R.drawable.promo, R.drawable.favoris

		};
		
		
		private String[] nThumbIds = { "Concours Internationnal",
				"Concours National", "Concours PromoGoal", "Favoris"
		};
	}
}